package com.handinfo.android.core.resource;

import java.io.ByteArrayInputStream;
import java.io.DataInputStream;

import com.handinfo.android.core.graphics.DWBitmap;
import com.handinfo.android.core.object.Actor;
import com.handinfo.android.core.object.Animation;
import com.handinfo.android.core.object.Background;
import com.handinfo.android.core.object.Scene;
import com.handinfo.android.game.Npc;
import com.handinfo.android.utils.Tools;

public class DWSerializableFactory
{
	public static final byte RESOURCE_TYPE_PNG = 1;
	public static final byte RESOURCE_TYPE_ANIM = 2;
	public static final byte RESOURCE_TYPE_BG = 3;
	public static final byte RESOURCE_TYPE_ACTOR = 4;
	public static final byte RESOURCE_TYPE_ROLE = 5;
	public static final byte RESOURCE_TYPE_NPC = 6;
	public static final byte RESOURCE_TYPE_ITEM = 7;
	public static final byte RESOURCE_TYPE_SCENE = 8;
	public static final byte RESOURCE_TYPE_SOUND = 9;

	public static final String DIR_IMG = "/img/";
	public static final String DIR_ANIM = "/anim/";
	public static final String DIR_BG = "/bg/";
	public static final String DIR_SCENE = "/scene/";
	public static final String DIR_AC = "/ac/";
	public static final String DIR_SOUND = "/sound/";
	public static final String EXTENSION_IMG = ".gnp";
	public static final String EXTENSION_ANIM = ".anim";
	public static final String EXTENSION_BG = ".bg";
	public static final String EXTENSION_SCENE = ".scn";
	public static final String EXTENSION_AC = ".ac";
	public static final String EXTENSION_SOUND = ".ogg";

	public static final DWSerializable convert(Resource res, ResHead head,
			byte[] data)
	{
		DWSerializable inst = null;
		switch (head.m_type) {
			case RESOURCE_TYPE_PNG:
				inst = new DWBitmap();
				break;
			case RESOURCE_TYPE_ANIM:
				inst = new Animation();
				break;
			case RESOURCE_TYPE_BG:
				inst = new Background();
				break;
			case RESOURCE_TYPE_SCENE:
				inst = new Scene();
				break;
			case RESOURCE_TYPE_ACTOR:
				inst = new Actor();
				break;
			case RESOURCE_TYPE_NPC:
				inst = new Npc();
				break;
		}

		try {
			ByteArrayInputStream bas = new ByteArrayInputStream(data);
			DataInputStream dis = new DataInputStream(bas);

			inst.read(res, dis);
			dis.close();
			bas.close();
		}
		catch (Exception e) {
			Tools.debugPrintln("type : " + head.m_type);
			Tools.debugPrintln("url : " + head.m_url);
			Tools.debug(e);
			return null;
		}
		return inst;
	}
}
